import { useRequest } from "ahooks";
import { specificationV1ControllerAll } from "@/services/auto/specification";
import { useMemo } from "react";

interface Right extends API.SpecificationAllResult{
  extra: {
    options?: {label: string;value: string}[];
    mapOptions?: Record<string, string>;
  }
}
const useSpecification = () => {
  const {
    data,
    loading
  } = useRequest(specificationV1ControllerAll);

  const mapData = useMemo<Record<string,
    Right
  > | undefined>(()=>{
    return data?.reduce((obj,item)=>{
      return {
        ...obj,
        [item.uuid]: {
          ...item,
          extra: {
            ...item.extra,
            mapOptions: item.extra.options?.reduce((obj2, it)=>{
              return {
                ...obj2,
                [it.value] : it.label,
              }
            }, {})
          }
        },
      }
    }, {})
  },[data])

  return {
    data,
    loading,
    mapData,
  };
};

export default useSpecification;
